package com.grace.storage.factory;

import java.util.HashMap;
import java.util.Map;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.stereotype.Component;
import com.grace.storage.enums.StorageEnum;
import com.grace.storage.enums.properties.StorageConfigProperties;
import com.grace.storage.service.StorageHandler;
import com.grace.storage.service.impl.NativeStorageHandler;
import com.grace.storage.service.impl.QiNiuStorageHander;
import com.grace.system.service.impl.SysSearchConfigServiceImpl;

import jakarta.annotation.PostConstruct;

@Component
public class StorageFactory {
    @Autowired
    private ApplicationContext applicationContext;
    @Autowired
    private SysSearchConfigServiceImpl configService;
    private Map<String, StorageHandler> fileRepoMap = new HashMap<>();

    @PostConstruct
    public void init() {
        fileRepoMap.put(StorageEnum.STORAGE_TYPE_NATIVE.getValue(), applicationContext.getBean(NativeStorageHandler.class));
        fileRepoMap.put(StorageEnum.STORAGE_TYPE_QN.getValue(), applicationContext.getBean(QiNiuStorageHander.class));
    }

    public StorageHandler get() {
        String scheme = configService.selectConfigDefaultValue(StorageConfigProperties.STORAGE_CONFIG_GROUP.getValue(),
                StorageConfigProperties.STORAGE_USED);
        return fileRepoMap.get(scheme);
    }
}
